#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/21 17:41
    Desc  :
--------------------------------------
"""


from flask import Blueprint, render_template, request
from application import app
from common.libs.JsonResponseUtils import JsonResponse
from common.libs.QuerySetUtils import QuerySetUtils
from common.libs.paginationUtils import ListPaginationUtils
from common.libs.queryListUtils import QueryListUtils
from servers.models.HeadersModel import Headers

headers_route = Blueprint('headers', __name__)


@headers_route.route('/list', methods = ["POST"])
def HeadersList():
    try:

        form = {} if not request.json else dict(request.json)
        page, size = form.get('page'), form.get('size')
        form = QueryListUtils(form).filter_by('icontains')
        # 1. 获取当前项目id
        projectId = request.headers.get('project')
        form['projectId'] = projectId
        user = Headers.objects(**form).order_by('-id')
        pagination = ListPaginationUtils(user, **form)
        return JsonResponse(code = 20000, data = pagination.items, message = '请求成功').jsonExit()
    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()


@headers_route.route('/add', methods = ["POST"])
def HeadersAdd():
    """
    新增
    :return:
    """
    try:
        form = {} if not request.json else dict(request.json)
        form = QuerySetUtils(Headers).filterField(form)
        # 1. 获取当前项目id
        projectId = request.headers.get('project')
        form['projectId'] = projectId
        if form.get('headersName') is None or form.get('headersName') == '':
            return JsonResponse(code = 40002, data = None, message = "HeadersName不能为空").jsonExit()
        case = Headers(**form)
        case.save()
        return JsonResponse(code = 20000, data = case, message = "请求成功").jsonExit()

    except Exception as e:

        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()


# 编辑
@headers_route.route('/edit', methods = ["POST"])
def HeadersEdit():

    try:

        form = {} if not request.json else dict(request.json)
        form = QuerySetUtils(Headers).filterField(form)
        if form.get("id") is None or form.get("id") == "":
            return JsonResponse(code = 40002, data = None, message = "id不能为空").jsonExit()
        case = Headers.objects(id = form.get('id'))
        if case.count() == 0:
            return JsonResponse(code = 40005, data = None, message = "用户不存在").jsonExit()
        case.update(**form)
        return JsonResponse(code = 20000, data = case, message = "请求成功").jsonExit()
    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()


# 删除
@headers_route.route('/delete', methods = ["POST"])
def HeadersDelete():

    try:
        form = {} if not request.json else dict(request.json)
        if form.get("id") is None or form.get("id") == "":
            return JsonResponse(code = 40002, data = None, message = "id不能为空").jsonExit()
        case = Headers.objects(id = form.get('id'))
        if case.count() == 0:
            return JsonResponse(code = 40005, data = None, message = "用户不存在").jsonExit()
        case.delete()
        return JsonResponse(code = 20000, data = None, message = "请求成功").jsonExit()
    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()


# 复制
@headers_route.route('/copy', methods=['POST'])
def HeadersCopy():
    """
    用例复制
    :return:
    """
    try:
        form = {} if not request.json else dict(request.json)
        form = QuerySetUtils(Headers).filterField(form)

        # 1. 获取当前项目id
        projectId = request.headers.get('project')
        form['projectId'] = projectId
        if form.get('headersName') is None or form.get('headersName') == '':
            return JsonResponse(code = 40002, data = None, message = "HeadersName不能为空").jsonExit()
        case = Headers(**form)
        case.save()
        return JsonResponse(code = 20000, data = case, message = "请求成功").jsonExit()

    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()